import { FORMAT_DATE, HIDDEN_COMPANY_IDS, COMPANY_NAME } from '@/utils/const';
import userStore from '@/store/modules/user';

// 注入组件内数据
const componentData = ref({});
export const injectData = obj => {
  componentData.value = { ...componentData.value, ...obj };
};

//  查询表单
export const searchParams = reactive({});

// 业务组查询条件
function setFormItems({ name }) {
  return reactive({
    name: {
      component: 'input',
      label: name,
      placeholder: `请输入${name}`,
      maxlength: 100
    }
  });
}

// 当前row
export const activeRow = ref({});
export const dialogName = ref('');
export const dialogInfo = ref({});

export const showApply_edit = ref(false); // 申请类型编辑
export const showBusiness_edit = ref(false); // 业务组编辑
export const showDomain_edit = ref(false); // 域名用途编辑
export const showDomain_add = ref(false); // 用途类型新增
export const showNumber_edit = ref(false); // 个号类型编辑
export const showProxy_edit = ref(false); // 代理商编辑

// 站点类型,域名初始化,站点模式,托管类型,邮箱用途,邮箱模板
// -编辑
export const showSitetype_edit = ref(false);

export const showEmailTemp_add = ref(false); // 邮箱模板-新增
export const showProxy_add = ref(false); // 代理商新增

export const addDomainType = () => {
  showDomain_add.value = true;
};

export const addEmailTemp = () => {
  showEmailTemp_add.value = true;
};

export const addProxy = () => {
  showProxy_add.value = true;
};

// 申请类型表格字段
const columns_applytype = reactive([
  { prop: 'id', label: '申请类型ID', fixed: 'left' },
  { prop: 'name', label: '申请类型', fixed: 'left' },
  { prop: 'belong_to', label: '所属类目' },
  {
    prop: 'visible_companies',
    label: '可见公司',
    format(data) {
      if (!data?.length) return undefined;
      const names = COMPANY_NAME(data);
      return names.join('、');
    }
  },
  {
    prop: 'visible_companies',
    label: '不可见公司',
    format(data) {
      const list = HIDDEN_COMPANY_IDS(data);
      if (!list?.length) return undefined;
      const names = COMPANY_NAME(list);
      return names.join('、');
    }
  },
  {
    prop: 'updater_id',
    label: '更新人',
    format(data) {
      const userMap = userStore().userMap || {};
      return userMap[data]?.label;
    }
  },
  {
    prop: 'updated_at',
    label: '更新时间',
    format(data) {
      return FORMAT_DATE(data);
    }
  },
  {
    label: '操作',
    width: '100px',
    fixed: 'right',
    buttons: [
      {
        label: '编辑',
        click({ row }) {
          activeRow.value = row;
          showApply_edit.value = true;
        }
      }
    ]
  }
]);

// 业务组表格字段
const columns_business = reactive([
  { prop: 'id', label: '业务组ID', fixed: 'left' },
  { prop: 'name', label: '业务组名称', fixed: 'left' },
  { prop: 'business_line', label: '所属业务线' },
  {
    prop: 'visible_companies',
    label: '可见公司',
    format(data) {
      if (!data?.length) return undefined;
      const names = COMPANY_NAME(data);
      return names.join('、');
    }
  },
  {
    prop: 'visible_companies',
    label: '不可见公司',
    format(data) {
      const list = HIDDEN_COMPANY_IDS(data);
      if (!list?.length) return undefined;
      const names = COMPANY_NAME(list);
      return names.join('、');
    }
  },
  { prop: 'code', label: '业务组code' },
  {
    prop: 'updater_id',
    label: '更新人',
    format(data) {
      const userMap = userStore().userMap || {};
      return userMap[data]?.label;
    }
  },
  {
    prop: 'updated_at',
    label: '更新时间',
    format(data) {
      return FORMAT_DATE(data);
    }
  },
  {
    label: '操作',
    width: '100px',
    fixed: 'right',
    buttons: [
      {
        label: '编辑',
        click({ row }) {
          activeRow.value = row;
          showBusiness_edit.value = true;
        }
      }
    ]
  }
]);

// 域名用途表格字段
const columns_domain = reactive([
  { prop: 'id', label: '域名用途ID', fixed: 'left' },
  { prop: 'name', label: '域名用途名称', fixed: 'left' },
  {
    prop: 'level',
    label: '用途类别',
    format(data) {
      if (data == 1) return '一级';
      if (data == 2) return '二级';
      return undefined;
    }
  },
  { prop: 'parent_name', label: '所属上级' },
  {
    prop: 'visible_companies',
    label: '可见公司',
    format(data, row) {
      if (row.level == 1) return undefined;
      if (!data?.length) return undefined;
      const names = COMPANY_NAME(data);
      return names.join('、');
    }
  },
  {
    prop: 'visible_companies',
    label: '不可见公司',
    format(data, row) {
      if (row.level == 1) return undefined;
      const list = HIDDEN_COMPANY_IDS(data);
      if (!list?.length) return undefined;
      const names = COMPANY_NAME(list);
      return names.join('、');
    }
  },
  {
    prop: 'updater_id',
    label: '更新人',
    format(data) {
      const userMap = userStore().userMap || {};
      return userMap[data]?.label;
    }
  },
  {
    prop: 'updated_at',
    label: '更新时间',
    format(data) {
      return FORMAT_DATE(data);
    }
  },
  {
    label: '操作',
    width: '100px',
    fixed: 'right',
    buttons: [
      {
        label: '编辑',
        // 一级类别不提供【编辑】按钮；
        // 二级类别，提供【编辑】按钮，点击后进入对应域名用途编辑弹窗；
        hidden({ row }) {
          return row.level == 1;
        },
        click({ row }) {
          activeRow.value = row;
          showDomain_edit.value = true;
        }
      }
    ]
  }
]);

// 个号类型表格字段
const columns_numbertype = reactive([
  { prop: 'id', label: '个号类型ID', fixed: 'left' },
  { prop: 'name', label: '个号类型', fixed: 'left' },
  { prop: 'price', label: '个号类型（元/个）' },
  {
    prop: 'visible_companies',
    label: '可见公司',
    format(data) {
      if (!data?.length) return undefined;
      const names = COMPANY_NAME(data);
      return names.join('、');
    }
  },
  {
    prop: 'visible_companies',
    label: '不可见公司',
    format(data) {
      const list = HIDDEN_COMPANY_IDS(data);
      if (!list?.length) return undefined;
      const names = COMPANY_NAME(list);
      return names.join('、');
    }
  },
  {
    prop: 'updater_id',
    label: '更新人',
    format(data) {
      const userMap = userStore().userMap || {};
      return userMap[data]?.label;
    }
  },
  {
    prop: 'updated_at',
    label: '更新时间',
    format(data) {
      return FORMAT_DATE(data);
    }
  },
  {
    label: '操作',
    width: '100px',
    fixed: 'right',
    buttons: [
      {
        label: '编辑',
        click({ row }) {
          activeRow.value = row;
          showNumber_edit.value = true;
        }
      }
    ]
  }
]);

// 代理商表格字段
const columns_proxy = reactive([
  { prop: 'id', label: '代理商ID', fixed: 'left' },
  { prop: 'name', label: '代理商', fixed: 'left' },
  {
    prop: 'updater_id',
    label: '更新人',
    format(data) {
      const userMap = userStore().userMap || {};
      return userMap[data]?.label;
    }
  },
  {
    prop: 'updated_at',
    label: '更新时间',
    format(data) {
      return FORMAT_DATE(data);
    }
  },
  {
    label: '操作',
    width: '100px',
    fixed: 'right',
    buttons: [
      {
        label: '编辑',
        click({ row }) {
          activeRow.value = row;
          showProxy_edit.value = true;
        }
      }
    ]
  }
]);

// 统一的表格字段
function setColumns({ name, columnNameLabel, dialogNameLabel }) {
  return reactive([
    { prop: 'id', label: `${name}ID`, fixed: 'left' },
    {
      prop: 'name',
      label: `${columnNameLabel || name + '名称'}`,
      fixed: 'left'
    },
    {
      prop: 'visible_companies',
      label: '可见公司',
      format(data) {
        if (!data?.length) return undefined;
        const names = COMPANY_NAME(data);
        return names.join('、');
      }
    },
    {
      prop: 'visible_companies',
      label: '不可见公司',
      format(data) {
        const list = HIDDEN_COMPANY_IDS(data);
        if (!list?.length) return undefined;
        const names = COMPANY_NAME(list);
        return names.join('、');
      }
    },
    {
      prop: 'updater_id',
      label: '更新人',
      format(data) {
        const userMap = userStore().userMap || {};
        return userMap[data]?.label;
      }
    },
    {
      prop: 'updated_at',
      label: '更新时间',
      format(data) {
        return FORMAT_DATE(data);
      }
    },
    {
      label: '操作',
      width: '100px',
      fixed: 'right',
      buttons: [
        {
          label: '编辑',
          click({ row }) {
            activeRow.value = row;
            dialogName.value = name;
            dialogInfo.value = {
              dialogNameLabel
            };
            showSitetype_edit.value = true;
          }
        }
      ]
    }
  ]);
}

// 查询字段抛出
export const formItems = computed(() => {
  return {
    sqlx: setFormItems({ name: '申请类型' }),
    ywz: setFormItems({ name: '业务组' }),
    zdlx: setFormItems({ name: '站点类型' }),
    ymyt: setFormItems({ name: '域名用途' }),
    ymcsh: setFormItems({ name: '域名初始化' }),
    zdms: setFormItems({ name: '站点模式' }),
    tglx: setFormItems({ name: '托管类型' }),
    yxyt: setFormItems({ name: '邮箱用途' }),
    yxmb: setFormItems({ name: '邮箱模板' }),
    ghlx: setFormItems({ name: '个号类型' }),
    fbggzhlx: setFormItems({ name: '广告账户类型' }),
    fbggdls: setFormItems({ name: '代理商' })
  }[componentData.value.activeTab];
});

// 表格字段抛出
export const columns = computed(() => {
  return {
    sqlx: columns_applytype,
    ywz: columns_business,
    ymyt: columns_domain,
    zdlx: setColumns({ name: '站点类型' }),
    ymcsh: setColumns({ name: '域名初始化' }),
    zdms: setColumns({ name: '站点模式' }),
    tglx: setColumns({ name: '托管类型' }),
    yxyt: setColumns({ name: '邮箱用途' }),
    yxmb: setColumns({ name: '邮箱模板' }),
    ghlx: columns_numbertype,
    fbggzhlx: setColumns({
      name: '广告账户类型',
      columnNameLabel: '广告账户类型',
      dialogNameLabel: '广告账户类型'
    }),
    fbggdls: columns_proxy
  }[componentData.value.activeTab];
});
